//
//  main.m
//  wz
//
//  Created by My Mac on 16/5/18.
//  Copyright © 2016年 My Mac. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSString * baseURL =@"www.baidu.com/s";
        NSDictionary * params = @{
                    @"wd":@"help",
        
        };
        
        NSMutableArray *arr = [NSMutableArray new];
        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSString *str = [NSString stringWithFormat:@"%@=%@",key,obj];
            [arr addObject:str];
        }];
        NSString *res = [arr componentsJoinedByString:@"&"];
        res = [res stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        NSLog(@"%@?%@",baseURL,res);
        
        res = [res stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        res = [NSString stringWithFormat:@"%@?%@",baseURL,res];
        NSLog(@"%@",res);
        
        NSURL *url = [NSURL URLWithString:res];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        NSError *err;
        NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];
        if (err) {
            NSLog(@"%@",err);
        }
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
    }
    return 0;
}
